import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Gao zhao xu
 * Date: 2023-12-18
 * Time: 上午 10:04
 */
public class Test2 {
    static class Edge {
        public int srcIndex;
        public int destIndex;
        public int weight;
        public Edge(int srcIndex, int destIndex, int weight) {
            this.srcIndex = srcIndex;
            this.destIndex = destIndex;
            this.weight = weight;
        }
    }
    public static int prime(int n, int[][] matrix) {
        int srcIndex = 0;
        //存储已经确定的点
        Set<Integer> setX = new HashSet<>();
        setX.add(srcIndex);
        //需要一个优先级队列来存储边
        PriorityQueue<Edge> minHeap =  new PriorityQueue<>((o1, o2) -> o1.weight - o2.weight);
        for (int i = 0; i < n; i++) {
            if (matrix[srcIndex][i] != Integer.MAX_VALUE) {
                minHeap.offer(new Edge(srcIndex, i,matrix[srcIndex][i]));
            }
        }
        int size = 0;
        int totalWeight = 0;
        // 遍历优先级队列，选出n - 1 条边
        while (!minHeap.isEmpty()) {
            Edge min = minHeap.poll();
            int destI = min.destIndex;
            if (!setX.contains(destI)) {
                size++;
                totalWeight += min.weight;
                if (size == n - 1) return totalWeight;
                setX.add(destI);
                for (int i = 0; i < n; i++) {
                    if (matrix[destI][i] != Integer.MAX_VALUE && !setX.contains(i)) {
                        minHeap.offer(new Edge(destI, i,matrix[destI][i]));
                    }
                }
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int n = input.nextInt();
        int[][] matrix = new int[n][n];
        for (int i = 0; i < n; i++) {
            Arrays.fill(matrix[i], Integer.MAX_VALUE);
        }
        for (int i = 0; i < n - 1; i++) {
            String srcV = input.next();
            int srcIndex = srcV.charAt(0) - 'A';
            int x = input.nextInt();
            for (int j = 0; j < x; j++) {
                String destV = input.next();
                int destIndex = destV.charAt(0) - 'A';
                int weight = input.nextInt();
                matrix[srcIndex][destIndex] = weight;
                matrix[destIndex][srcIndex] = weight;
            }
        }
        System.out.println(prime(n, matrix));
    }
}
